package com.learner.course.controller.api;


import com.learner.common.util.JwtUtils;
import com.learner.course.service.CourseCollectService;
import com.learner.pojo.course.expand.CourseCollectVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Description: 收藏课程的控制器
 * @Author: ice2020x
 * @Date: 2021/10/28
 */
@RestController
@CrossOrigin
@RequestMapping("/api/edu/collect")
public class ApiCourseCollectController {

    @Autowired
    CourseCollectService courseCollectService;

    /**
     * 收藏课程
     *
     * @param courseId 课程id
     * @param request  request
     */
    @PostMapping("/auth/save/{courseId}")
    public void save(@PathVariable("courseId") String courseId, HttpServletRequest request) {
        String userId = JwtUtils.getCurrentUserId(request);
         courseCollectService.save(courseId, userId);
    }
    /**
     * 判断该课程是否收藏
     *
     * @param courseId 课程id
     * @param request  request
     * @return 结果
     */
    @GetMapping("auth/is_collect/{courseId}")
    public boolean isCollect(@PathVariable("courseId") String courseId, HttpServletRequest request) {
        String userId = JwtUtils.getCurrentUserId(request);
        return  courseCollectService.isCollect(courseId, userId);
    }


    /**
     * 根据课程id和会员id取消收藏
     *
     * @param courseId 课程id
     * @param request  request
     * @return 结果
     */
    @DeleteMapping("/auth/remove/{courseId}")
    public int remove(@PathVariable("courseId") String courseId, HttpServletRequest request) {
        String userId = JwtUtils.getCurrentUserId(request);
        return courseCollectService.removeCourseCollect(courseId, userId);
    }


    /**
     * 获取课程收藏列表
     *
     * @param request request
     * @return 结果
     */
    @GetMapping("/auth/list")
    public List<CourseCollectVo> getCourseCollectList(HttpServletRequest request) {
        String userId = JwtUtils.getCurrentUserId(request);
        return courseCollectService.getCourseCollectList(userId);
    }


}
